/*
 * Copyright (C) 2004, Velocityme Partnership
 * TaskTimeActionForm.java
 *
 * Created on 06 September 2004, 05:16
 */

package com.velocityme.www.actionforms;

import com.velocityme.session.ActivityLocal;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.StateLocal;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;


/**
 *
 * @author  Robert
 */
public class TimeActionForm extends ActionForm {
    
    private String description;
    private long activityId;
    private long stateId;
    private byte percentageComplete;
    private int year;
    private int month;
    private int day;
    private int durationDays;
    private int durationHours;
    private int durationMins;
    private Time m_startTime = new Time();
    private Time m_stopTime = new Time();
    
    public void extractTime(KeyLocal key, com.velocityme.entity.Time time) {
        time.setDescription(description);
        time.setStartTime(getStartDate());
        time.setStopTime(getStopDate());
        time.setRemainingDuration_min(getDurationTotalMins());
        time.setActivity(lookupActivityBeanLocal().findActivity(key, activityId));
        time.setState(lookupStateBeanLocal().findState(key, stateId));
        time.setPerson(key.getPerson());
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getStartDate().after(getStopDate())) {
            errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.start.before.stop"));
            return errors;
        }
        return super.validate(mapping, request);
    }
    
    public String getDescription() { return description; }
    
    public void setDescription(String description) { this.description = description; }
    
    public byte getPercentageComplete() { return percentageComplete; }
    
    public void setPercentageComplete(byte percentageComplete) { this.percentageComplete = percentageComplete; }
    
    public long getActivityId() { return activityId; }
    
    public void setActivityId(long taskId) { activityId = taskId; }
    
    public long getStateId() { return stateId; }
    
    public void setStateId(long stateId) { this.stateId = stateId; }
    
    public int getYear() { return year; }
    
    public void setYear(int year) { this.year = year; }
    
    public int getMonth() { return month; }
    
    public void setMonth(int month) { this.month = month; }
    
    public int getDay() { return day; }
    
    public void setDay(int day) { this.day = day; }
    
    public int getStartHour() { return m_startTime.getHour(); }
    
    public void setStartHour(int startHour) { m_startTime.setHour(startHour); }
    
    public int getStartMinute() { return m_startTime.getMinute(); }
    
    public void setStartMinute(int startMinute) { m_startTime.setMinute(startMinute); }
    
    public int getStopHour() { return m_stopTime.getHour(); }
    
    public void setStopHour(int stopHour) { m_stopTime.setHour(stopHour); }
    
    public int getStopMinute() { return m_stopTime.getMinute(); }
    
    public void setStopMinute(int stopMinute) { m_stopTime.setMinute(stopMinute); }
    
    public int getDurationDays() { return durationDays; }
    
    public void setDurationDays(int durationDays) { this.durationDays = durationDays; }
    
    public int getDurationHours() { return durationHours; }
    
    public void setDurationHours(int durationHours) { this.durationHours = durationHours; }
    
    public int getDurationMins() { return durationMins; }
    
    public void setDurationMins(int durationMins) { this.durationMins = durationMins; }
    
    private int getDurationTotalMins() {
        return durationDays * 8 * 60 + durationHours * 60 + durationMins;
    }
    
    public void setDurationTotalMins(int totalMins) {
        durationDays = totalMins / (8 * 60);
        durationHours = (totalMins / 60) % 8;
        durationMins = totalMins % 60;
    }
    
    private class Time {
        private int m_hour;
        private int m_minute;
        
        public void setTime(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DATE);
            m_hour = calendar.get(Calendar.HOUR_OF_DAY);
            m_minute = calendar.get(Calendar.MINUTE) / 5 * 5;
        }
        
        public Date getDate() {
            Calendar date = Calendar.getInstance();
            date.clear();
            date.set(year, month, day, m_hour, m_minute, 0);
            return date.getTime();
        }
        
        public int getHour() { return m_hour; }
        public void setHour(int hour) { m_hour = hour; }
        public int getMinute() { return m_minute; }
        public void setMinute(int minute) { m_minute = minute; }
    }
    
    public Date getStartDate() {
        return m_startTime.getDate();
    }
    
    public void setStartDate(Date start) {
        m_startTime.setTime(start);
    }
    
    public Date getStopDate() {
        return m_stopTime.getDate();
    }
    
    public void setStopDate(Date stop) {
        m_stopTime.setTime(stop);
    }

    private StateLocal lookupStateBeanLocal() {
        try {
            Context c = new InitialContext();
            return (StateLocal) c.lookup("java:comp/env/StateBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private ActivityLocal lookupActivityBeanLocal() {
        try {
            Context c = new InitialContext();
            return (ActivityLocal) c.lookup("java:comp/env/ActivityBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
